package logic

import (
	"context"
	"house_srv/mysql"
	"house_srv/pkg"

	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRegisterLogic) UserRegister(in *house.UserRegisterRequest) (*house.UserRegisterResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{
		Mobile:   in.Mobile,
		Password: pkg.Md5Encryption(in.Password),
	}
	err := u.UserGetMobile(in.Mobile)
	if err != nil {
		logx.Info("手机号不正确，请重新注册", err)
		return nil, err
	}
	err = u.Create()
	if err != nil {
		logx.Info("创建用户信息失败", err)
		return nil, err
	}
	return &house.UserRegisterResponse{
		UserId: int64(u.ID),
	}, nil
}
